import json
import math


def get_total_list(input_list):
    total_list, name_list, class_list = [], [], []
    total_object = ["DIS1", "DIS2", "DIS3", "DIS4", "DIS5", "DIS6", "DIS7", "DIS8", 'DIV']
    for image_num in range(len(input_list)):
        # print(input_list[image_num])
        image_list=[]
        class_name_list = [int(input_list[image_num]['imageId'].split('_')[0]), input_list[image_num]['imageId']]
        name_list.append(input_list[image_num]['imageId'])
        class_list.append(int(input_list[image_num]['imageId'].split('_')[0]))
        image_list.append(class_name_list)
        x1, y1, x2, y2 = map(float, input_list[image_num]['object_detect']['object']['0']['coordinate'])
        x3, y3, x4, y4 = map(float, input_list[image_num]['object_detect']['object']['1']['coordinate'])
        x5, y5 = map(float, input_list[image_num]['object_detect']['point']['0']['position'])
        x6, y6 = map(float, input_list[image_num]['object_detect']['point']['1']['position'])
        for index, object in enumerate(total_object):
            if object == 'DIS1':
                p = [object, round((x1 - x3))]
            elif object == 'DIS2':
                p = [object, round((y1 - y3))]
            elif object == 'DIS3':
                p = [object, round((x2 - x4))]
            elif object == 'DIS4':
                p = [object, round((y2 - y4))]
            elif object == 'DIS5':
                p = [object, round((x6 - x5))]
            elif object == 'DIS6':
                p = [object, round((y6 - y5))]
            elif object == 'DIS7':
                p = [object, round((y5 - y3))]
            elif object == 'DIS8':
                p = [object, round((y6 - y1))]
            else:
                area1 = math.fabs(x2 - x1) * math.fabs(y2 - y1)
                area2 = math.fabs(x4 - x3) * math.fabs(y4 - y3)
                p = [object, round((area2 / area1), 1)]
            image_list.append(p)
        total_list.append(image_list)
    return total_list, name_list, class_list


def predicate_to_seq(features_list, seg=3):
    features_seq = []
    total_object = ["DIS1", "DIS2", "DIS3", "DIS4", "DIS5", "DIS6", "DIS7", "DIS8", 'DIV']
    num = 0
    dmap = {}
    for x in total_object:
        dmap[x] = num
        num = num + 100
    class_name, img_name = features_list[0][0], features_list[0][1]
    for i in range(1, len(features_list)):
        if features_list[i][0].startswith('DIS'):
            features_seq.append(dmap[features_list[i][0]] + (features_list[i][1] + 100) // seg)
        else:
            features_seq.append(int(dmap[features_list[i][0]] + features_list[i][1] * 10))
    return features_seq, img_name, class_name


def data_processing(data_path):
    features_seq_list = []
    with open(data_path, 'r') as f:
        data = json.load(f)
    total_list, name_list, class_list = get_total_list(data)
    for features_list in total_list:
        features_seq, img_name, class_name = predicate_to_seq(features_list)
        features_seq_list.append(features_seq)
    return features_seq_list, name_list, class_list




# if __name__ == '__main__':
#     json_path = "../data/bladder_train1701.json"
#     features_seq_list, name_list, class_list = data_processing(json_path)
#     print(features_seq_list, name_list, class_list)
